package kr.hkit.android_caloriecheck.fragment;

import java.util.ArrayList;
import java.util.Calendar;

import kr.hkit.android_caloriecheck.R;
import kr.hkit.android_caloriecheck.adapter.CalendarAdapter;
import kr.hkit.android_caloriecheck.dto.DayInfo;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;

public class DiaryFragment extends Fragment {
	int SUNDAY = 1;
	TextView title;
	GridView calendar;
	ArrayList<DayInfo> mDayList;
	CalendarAdapter mCalendarAdapter;
	Calendar mThisMonthCalendar;
	
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View root = inflater.inflate(R.layout.diary, container, false);

		Button bPrevMonth = (Button) root.findViewById(R.id.prev);
		Button bNextMonth = (Button) root.findViewById(R.id.next);

		title = (TextView) root.findViewById(R.id.title);
		calendar = (GridView) root.findViewById(R.id.calendar);

		mDayList = new ArrayList<DayInfo>();

		mThisMonthCalendar = Calendar.getInstance();
		mThisMonthCalendar.set(Calendar.DAY_OF_MONTH, 1);
		getCalendar(mThisMonthCalendar);

		bPrevMonth.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				mThisMonthCalendar = getLastMonth(mThisMonthCalendar);
				getCalendar(mThisMonthCalendar);
			}
		});

		bNextMonth.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				mThisMonthCalendar = getNextMonth(mThisMonthCalendar);
				getCalendar(mThisMonthCalendar);
			}
		});

		calendar.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {				
				SharedPreferences pref = getActivity().getSharedPreferences("date", 0);
				SharedPreferences.Editor edit = pref.edit();
				edit.putInt("year", mDayList.get(position).getYear());
				edit.putInt("month", mDayList.get(position).getMonth());
				edit.putInt("day", mDayList.get(position).getDay());
				edit.commit();
				
				FragmentManager fm = getActivity().getFragmentManager();
				FragmentTransaction ft = fm.beginTransaction();
				Fragment prev = fm.findFragmentByTag("dialog");
				if(prev != null){
					ft.remove(prev);
				}
				DayFragment dialog = new DayFragment();
				dialog.show(ft, "dialog");
			}
		});		
		
		return root;
	}
	
	private void getCalendar(Calendar calendar) {
		int prevMonthStartDay;
		int dayOfMonth;
		int thisMonthLastDay;

		mDayList.clear();

		// 이번달 시작일의 요일을 구한다. 시작일이 일요일인 경우 인덱스를 1(일요일)에서 8(다음주 일요일)로 바꾼다.
		dayOfMonth = calendar.get(Calendar.DAY_OF_WEEK);
		thisMonthLastDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
		
		calendar.add(Calendar.MONTH, -1);

		
		// 지난달의 마지막 일자를 구한다.
		prevMonthStartDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);

		calendar.add(Calendar.MONTH, 1);

		if (dayOfMonth == SUNDAY) {
			dayOfMonth += 7;
		}

		prevMonthStartDay -= (dayOfMonth - 1) - 1;

		title.setText(mThisMonthCalendar.get(Calendar.YEAR) + "년 "
				+ (mThisMonthCalendar.get(Calendar.MONTH) + 1) + "월");

		DayInfo day;

		for (int i = 0; i < dayOfMonth - 1; i++) {
			int date = prevMonthStartDay + i;
			day = new DayInfo();
			day.setDay(date);
			day.setInMonth(false);
			day.setMonth(mThisMonthCalendar.get(Calendar.MONTH));
			day.setYear(mThisMonthCalendar.get(Calendar.YEAR));
			mDayList.add(day);
		}
		
		for (int i = 1; i <= thisMonthLastDay; i++) {
			day = new DayInfo();
			day.setDay(i);
			day.setInMonth(true);
			day.setMonth(mThisMonthCalendar.get(Calendar.MONTH) + 1);
			day.setYear(mThisMonthCalendar.get(Calendar.YEAR));
			mDayList.add(day);
		}
		
		for (int i = 1; i < 42 - (thisMonthLastDay + dayOfMonth - 1) + 1; i++) {
			day = new DayInfo();
			day.setDay(i);
			day.setInMonth(false);
			day.setMonth(mThisMonthCalendar.get(Calendar.MONTH) + 2);
			day.setYear(mThisMonthCalendar.get(Calendar.YEAR));
			mDayList.add(day);
		}
		
		initCalendarAdapter();
	}

	private Calendar getLastMonth(Calendar calendar) {
		calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
				1);
		calendar.add(Calendar.MONTH, -1);
		title.setText(mThisMonthCalendar.get(Calendar.YEAR) + "년 "
				+ (mThisMonthCalendar.get(Calendar.MONTH) + 1) + "월");
		return calendar;
	}

	private Calendar getNextMonth(Calendar calendar) {
		calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
				1);
		calendar.add(Calendar.MONTH, +1);
		title.setText(mThisMonthCalendar.get(Calendar.YEAR) + "년 "
				+ (mThisMonthCalendar.get(Calendar.MONTH) + 1) + "월");
		return calendar;
	}

	private void initCalendarAdapter() {
		mCalendarAdapter = new CalendarAdapter(getActivity(), R.layout.day,
				mDayList);
		calendar.setAdapter(mCalendarAdapter);
	}

}
